package development;

import java.util.Vector;

import state.DrawListener;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TouchScreenListener;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.component.GridMenu;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import development.bean.Room;
import development.group.FrmGroupFriendAndTop;
import development.group.FrmGroupListTableFreeUserChat;
import development.group.FrmGroupMenu;
import development.group.FrmGroupNapXuAndExchangeGold;
import development.socket.SocketClientUtil;

public class FrmListRoom extends GameForm implements EventListener, TouchScreenListener {
  private int DISTANCE = 23;
  private int DX = 7;
  private int DY = 90;
  private int TAB_DX = 7;
  private int TAB_DY = 32;

  private GameImage tabImage;
  private GameImage tabFocusImage;
  private GameImage markupImage;
  private GameImage popupImage;

  private ImageText text8;
  private int[] rowDy;
  private String[] roomTypeName;
  private int tabIndex;
  private GridMenu gridMenu;
  private Sprite[] tabs;

  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmListRoom();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    SocketClientUtil.getRoomList(Global.currentGame.getId());
    popupImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_popup.png");
    tabImage = ImageUtil.getImage(Constants.MANDATORY_CATEGORY, "bg_Tab.png");
    tabFocusImage = ImageUtil.getImage(Constants.MANDATORY_CATEGORY, "bg_Tab-yellow.png");
    markupImage = ImageUtil.getImage(Constants.MANDATORY_CATEGORY, "icon_list-room.png");
    
    roomTypeName = new String[]{"SƠ CẤP", "TRUNG CẤP", "VIP"};
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    tabIndex = 0;
    
    // Create bottom bar
    GameMenu menu = new GameMenu(null, new String[] {"Update danh sách", "Tự động tìm bàn", "Mời chơi", "Bạn bè", "Nạp xu", "Đổi gold", "Top cao thủ", "Top đại gia"}, 8, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
    bottomBar.setLabels("Menu", "Chọn", "Trở về");
    bottomBar.setLeftMenu("Menu", menu);
    
    if (Constants.IS_240x320_SCREEN) {
      DISTANCE = 23;
      DX = 7;
      DY = 90;
      TAB_DX = 7;
      TAB_DY = 32;
    } else {
      DISTANCE = 23;
      DX = 10;
      DY = 90;
      TAB_DX = 10;
      TAB_DY = 32;
    }
    rowDy = new int[]{DY, DY, DY};
    gridMenu = new GridMenu(_manager, null, null, 1, DX, DY, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE, this);
    
    tabs = new Sprite[3];
    for (int i = 0; i < 3; i++) {
      tabs[i] = new Sprite(null, _manager, TAB_DX + i * 76, TAB_DY, Constants.TOP_LEFT_ANCHOR);
      tabs[i].setSize(74, 45);
      tabs[i].setId((byte)i);
      tabs[i].setDrawListener(new DrawListener() {
        public void paint(Sprite source, Graphics g) {
          if (tabImage == null || tabFocusImage == null) {
            return;
          }
          
          Vector rooms = Global.currentGame.getRoomListByType(tabIndex);
          int i1 = source.getId();
          if (i1 == tabIndex) {
            g.drawImage(tabFocusImage, TAB_DX + i1 * (tabImage.getWidth() + 2), TAB_DY, GameConstants.TOP_LEFT_ANCHOR);
          } else {
            g.drawImage(tabImage, TAB_DX + i1 * (tabImage.getWidth() + 2), TAB_DY, GameConstants.TOP_LEFT_ANCHOR);
          }
          text8.drawString2(g, roomTypeName[i1], GameColor.WHITE_CODE, TAB_DX + 34 + i1 * (tabImage.getWidth() + 2), TAB_DY + 12, GameConstants.CENTER_ANCHOR);
          if (i1 == tabIndex && rooms.size() > 0) {
            Room room = (Room) rooms.elementAt(0);
            text8.drawString2(g, "(" + room.getMinBid() + "-" + room.getMaxBid() + ")", GameColor.WHITE_CODE, TAB_DX + 34 + i1 * (tabImage.getWidth() + 4), TAB_DY + 30, GameConstants.CENTER_ANCHOR);
          }
        }
      });
      tabs[i].setTouchScreenListener(this);
    }
    
    updateRoomList();
    SocketClientUtil.leaveTableRequest();
  }
  
  public void pointerDragged(Sprite source, int x1, int y1) {
  }

  public void pointerPressed(Sprite source, int x1, int y1) {
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    tabIndex = source.getId();
    updateRoomList();
  }
  
  public void updateRoomList() {
    if (gridMenu == null) {
      return;
    }
    
    Vector rooms = Global.currentGame.getRoomListByType(tabIndex);
    gridMenu.clearData();
    for (int i = 0; i < rooms.size(); i++) {
      Sprite sprite = new Sprite(null, null, 0, 0, Constants.TOP_LEFT_ANCHOR);
      sprite.setSize(GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE);
      sprite.setId((byte) i);
      gridMenu.addSpriteItem(sprite, null);
      sprite.setDrawListener(new DrawListener() {
        public void paint(Sprite source, Graphics g) {
          int i1 = source.getId();
          Vector rooms1 = Global.currentGame.getRoomListByType(tabIndex);
          if (i1 >= rooms1.size()) {
            return;
          }
          g.setColor(GameColor.WHITE_CODE);
          g.drawLine(DX, rowDy[tabIndex] - DISTANCE / 2 + i1 * DISTANCE, GameConstants.SCREEN_WIDTH - DX, rowDy[tabIndex] - DISTANCE / 2 + i1 * DISTANCE);
          Room room = (Room) rooms1.elementAt(i1);
          String roomName = "Phòng " + room.getName() + " (" + room.getConcurrentUser() + "/" + room.getMaxUser() + ")";
          int color = GameColor.WHITE_CODE;
          if (i1 == gridMenu.getCurrentSelectedIndex()) {
            g.setColor(0xCC999A);
            g.fillRect(DX, rowDy[tabIndex] - DISTANCE / 2 + i1 * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
            color = GameColor.BLACK_CODE;
          }
          g.drawImage(markupImage, DX + 4, rowDy[tabIndex] + i1 * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
          text8.drawString2(g, roomName, color, 26, rowDy[tabIndex] + i1 * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
        }
      });
    }
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    DrawUtils.drawTopBar(g);
    text8.drawString2(g, "Phòng chơi " + Global.currentGame.getName(), GameColor.WHITE_CODE, Constants.SCREEN_WIDTH / 2, 5, Constants.TOP_HCENTER_ANCHOR);

    if (Global.currentGame.numberOfChilds() == 0) {
      gridMenu.setVisible(false);
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Đang cập nhật danh sách phòng", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
    } else {
      gridMenu.setVisible(true);
    }
  }

  public void onActionPerform(Event event) {
    if (event.getSource() == gridMenu) {
      chooseItem(gridMenu.getCurrentSelectedIndex());
      return;
    }
    
    // "Cập nhật danh sách", "Tự động tìm bàn", "Mời chơi", "Bạn bè", "Nạp xu", "Đổi gold", "Top cao thủ", "Top đại gia"
    String action = event.getAction();
    if (action.equals("Update danh sách")) {
      SocketClientUtil.getRoomList(Global.currentGame.getId());
    } else if (action.equals("Tự động tìm bàn")) {
      SocketClientUtil.autoFindTable();
    } else if (action.equals("Bạn bè")) {
      FrmGroupFriendAndTop.backForm = this;
      GameGlobal.nextState(FrmGroupFriendAndTop.instance(), null);
    } else if (action.equals("Mời chơi")) {
      Global.nextState(FrmListFreeUser.instance(), null);
    } else if (action.equals("Nạp xu")) {
      FrmGroupNapXuAndExchangeGold.backForm = FrmListRoom.instance();
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
    } else if (action.equals("Đổi gold")) {
      FrmGroupNapXuAndExchangeGold.backForm = FrmListRoom.instance();
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), null);
      FrmGroupNapXuAndExchangeGold.setTab(1);
    } else if (action.equals("Top cao thủ")) {
      Global.nextState(FrmGroupFriendAndTop.instance(), null);
      FrmGroupFriendAndTop.backForm = FrmListRoom.instance();
      FrmGroupFriendAndTop.setTab(2);
    } else if (action.equals("Top đại gia")) {
      Global.nextState(FrmGroupFriendAndTop.instance(), null);
      FrmGroupFriendAndTop.setTab(1);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.RIGHT:
      if (Global.currentGame.numberOfChilds() == 0) {
        return;
      }
      tabIndex = (tabIndex + 1) % 3;
      gridMenu.setCurrentSelectedIndex(0);
      break;
    case Key.LEFT:
      if (Global.currentGame.numberOfChilds() == 0) {
        return;
      }
      tabIndex--;
      if (tabIndex < 0) {
        tabIndex = 2;
      }
      gridMenu.setCurrentSelectedIndex(0);
      break;
    case Key.FIRE:
    case Key.K_5:
      chooseItem(gridMenu.getCurrentSelectedIndex());
      break;
    case Key.SOFT_RIGHT:
      GameGlobal.nextState(FrmGroupMenu.instance(), null);
      break;
    }
  }
  
  private void chooseItem(int index) {
    if (Global.currentGame.getRoomListByType(tabIndex).size() == 0) {
      return;
    }
    
    Global.currentRoom = Global.currentGame.getChildByType(tabIndex, index);
    SocketClientUtil.joinRoomRequest(Global.currentRoom.getId());
    GameGlobal.nextState(FrmGroupListTableFreeUserChat.instance(), null);
  }

  public void destroy() {
    popupImage = null;
    text8 = null;
    rowDy = null;
    roomTypeName = null;
    gridMenu = null;
    tabs = null;
    tabImage = null;
    tabFocusImage = null;
    markupImage = null;
  }
}
